{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "def Pass():\n",
    "    ..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "{'name': 'quanwei', 'age': '18', 'gender': 'male'}\n",
      "{'name': 'quanwei', 'age': '18', 'gender': 'male'}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'name': 'quanwei', 'age': '18', 'gender': 'male'}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import itertools\n",
    "from collections import ChainMap \n",
    "from copy import deepcopy\n",
    "d1 = {'name': 'quanwei', 'age': '18'}\n",
    "d2 = {'gender': 'male'}   \n",
    "fd = deepcopy(d1) # 拷贝地址而不是类容\n",
    "print(fd is d1)\n",
    "fd.update(d2)\n",
    "print(fd)\n",
    "# ** unzip dict\n",
    "fd1 = {**d1, **d2}  \n",
    "print(fd1)\n",
    "\n",
    "dict(itertools.chain(d1.items(), d2.items()))\n",
    "dict(ChainMap(d1, d2))\n",
    "# same key get the first\n",
    "#  '|' 合并操作符\n",
    "d1 | d2\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['age', 'gender', 'name']\n",
      "['age', 'gender', 'name']\n",
      "[('age', '十八'), ('gender', 'man'), ('name', '张三')]\n",
      "[('age', '十八'), ('gender', 'man'), ('name', '张三')]\n",
      "['man', '十八', '张三']\n",
      "[('gender', 'man'), ('age', '十八'), ('name', '张三')]\n"
     ]
    }
   ],
   "source": [
    "# 字典排序 sorted()默认是按照升序排列，并且是按照ASCII码表的顺序排列的\n",
    "sys = {'name': '张三',\n",
    "       'age': '十八',\n",
    "       'gender': 'man'}\n",
    "#  单独打印出排序后的key值\n",
    "new_sys = sorted(sys) #以列表的形式返回\n",
    "print(new_sys)\n",
    "\n",
    "new_sys = sorted(sys.keys())\n",
    "print(new_sys)\n",
    "\n",
    "# 根据key的升序排列，把key value都打印出来\n",
    "new_sys1 = sorted(sys.items(), key=lambda d: d[0], reverse=False)\n",
    "print(new_sys1)\n",
    "\n",
    "new_sys1 = sorted(sys.items(), reverse=False)\n",
    "print(new_sys1)\n",
    "# 单独打印出排序后的value值\n",
    "new_sys1 = sorted(sys.values())\n",
    "print(new_sys1)\n",
    "\n",
    "# 打印出根据value排序后的键值对的具体值\n",
    "new_sys2 = sorted(sys.items(),  key=lambda d: d[1], reverse=False)\n",
    "print(new_sys2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1: 2, 2: 1, 3: 1, 4: 2}"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# add element into dict\n",
    "dct={}\n",
    "for i in [1,1,2,3,4,4]:\n",
    "    dct[i]=dct.get(i,0)+1\n",
    "dct"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [],
   "source": [
    "np=__import__('numpy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "Dog\n",
      "True\n",
      "Cat\n"
     ]
    }
   ],
   "source": [
    "# condition statement\n",
    "# <condition> and <on_ture> or <on_flase>\n",
    "print(1 > 2 and \"True\" or \"False\")\n",
    "\n",
    "# (<onfalse>,<on_true>)[condition]\n",
    "print((\"Dog\", \"Cat\")[1 > 2])\n",
    "\n",
    "# (lambda:\"False\",lambda:\"True\")[condition]()\n",
    "print((lambda: \"False\", lambda: \"True\")[2 > 1]())\n",
    "\n",
    "print({True: \"Dog\", False: \"Cat\"}[1 > 2])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "成年了\n",
      "成年了\n",
      "成年了\n",
      "成年了\n",
      "<class 'bool'>\n",
      "y\n"
     ]
    }
   ],
   "source": [
    "# 赋值表达式 Assignment Expressions\n",
    "if (age := 20) > 18:\n",
    "    print(\"成年了\")\n",
    "\n",
    "print((\"未成年\", \"成年了\")[(age := 20) > 18])\n",
    "print([\"未成年\", \"成年了\"][(age := 20) > 18])\n",
    "print({False:\"未成年\",True: \"成年了\"}[(age := 20) > 18])\n",
    "print(type((age := 20) > 18))\n",
    "\n",
    "\n",
    "while((p := input()) != \"y\"):\n",
    "    print(p)\n",
    "    continue\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "import ast \n",
    "expr = \"[1, 2, 3]\"\n",
    "my_list = ast.literal_eval(expr) \n",
    "print(my_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello James !\n"
     ]
    }
   ],
   "source": [
    "print( \"Hello %(name)s !\" % {'name': 'James'})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first, second, *rest, last = range(10)\n",
    "first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 16, 100, 4, 9]\n"
     ]
    }
   ],
   "source": [
    "num = [1, 4, -5, 10, -7, 2, 3, -1]\n",
    "filtered_and_squared = map(lambda x: x ** 2, filter(lambda x: x > 0, num))\n",
    "print(list(filtered_and_squared))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 16, 100, 4, 9]\n"
     ]
    }
   ],
   "source": [
    "num = [1, 4, -5, 10, -7, 2, 3, -1]\n",
    "filtered_and_squared = [x**2 for x in num if x > 0]\n",
    "print(list(filtered_and_squared))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "g: 1\n",
      "g: 1\n",
      "g: 2\n",
      "g: 3\n",
      "g: 5\n",
      "g: 8\n",
      "Generator return value: done\n"
     ]
    }
   ],
   "source": [
    "gen = (i for i in range(-20, 21) if i % 2 == 0)\n",
    "for i in gen:\n",
    "    # print(i)\n",
    "    ...\n",
    "    \n",
    "\n",
    "\n",
    "def fib(max):\n",
    "    n, a, b = 0, 0, 1\n",
    "    while n < max:\n",
    "        print(b)\n",
    "        a, b = b, a + b\n",
    "        n = n + 1\n",
    "    return 'done'\n",
    "\n",
    "# 一个函数定义中包含yield关键字，\n",
    "# 那么这个函数就不再是一个普通函数，而是一个generator\n",
    "\n",
    "\n",
    "def fib(max):\n",
    "    n, a, b = 0, 0, 1\n",
    "    while n < max:\n",
    "        yield b\n",
    "        a, b = b, a + b\n",
    "        n = n + 1\n",
    "    return 'done'\n",
    "\n",
    "\n",
    "# 如果想要拿到返回值，必须捕获StopIteration错误，\n",
    "# 返回值包含在StopIteration的value中\n",
    "g = fib(6)\n",
    "while True:\n",
    "    try:\n",
    "        x = next(g)\n",
    "        print('g:', x)\n",
    "    except StopIteration as e:\n",
    "        print('Generator return value:', e.value)\n",
    "        break\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "int_dict={'a':1,'b':2,'c':3}\n",
    "for i in int_dict.values():\n",
    "    print(i)"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "幻灯片",
  "kernelspec": {
   "display_name": "Python 3.9.7 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "metadata": {
   "interpreter": {
    "hash": "ac59ebe37160ed0dfa835113d9b8498d9f09ceb179beaac4002f036b9467c963"
   }
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  },
  "vscode": {
   "interpreter": {
    "hash": "42d820d8758cde2badbeb8f7eaaa44975ccec32144b366942c17254cc50801f8"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
